<!--
 * @Descripttion: 入库单
 * @version:
 * @Author: cxguo
 * @Date: 2019-08-02 15:03:05
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-12 15:02:05
 -->
<template>
  <div>
    <el-card shadow="never" class="oper-btn-card">
      <div class="clearfix">
        <el-form
          class="fl form-no-bottom"
          :inline="true"
          :model="searchData"
          label-position="left"
          label-width="80px"
          @keydown.enter.native="btnSearch"
        >
          <el-form-item>
            <el-checkbox
              v-model="searchData.isCancle"
              false-label="1"
              true-label="0"
              border
              @change="()=>{listData()}"
            >
              不显示已作废单据
            </el-checkbox>
            <el-input
              v-model="searchData.keywords"
              clearable
              style="width: 300px"
              placeholder="按单据号,商品名,供应商,备注"
            />
            <el-button type="primary" :loading="btnLoading.search" @click="btnSearch()">
              搜索
            </el-button>
            <el-button type="default" @click="isFilterOpen = !isFilterOpen">
              {{ btnFilterOpenText }}
            </el-button>
          </el-form-item>
        </el-form>
        <div class="fr">
          <!-- <el-button type="default" @click="btnExportData">导出</el-button> -->
        </div>
      </div>
      <SearchFilter
        v-show="isFilterOpen"
        :clear="btnClearConditions"
      >
        <Conditions ref="conditions" :query-data="searchData" />
      </SearchFilter>
    </el-card>
    <div class="margin-t2">
      <vxe-grid
        ref="table"
        class="grid"
        border
        resizable
        highlight-hover-row
        :row-class-name="(({row}) => { return row.isCancle === '1' ? 'is-cancle' : ''})"
        :loading="tableLoading.table"
        :columns="columnsHasInData"
        :data="tableData.table"
      />
    </div>
    <div class="clearfix">
      <div class="pagination-container">
        <el-pagination
          background
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="pageInfo.rows"
          :page-sizes="[20,30,40]"
          :current-page.sync="pageInfo.page"
          :total="pageInfo.total"
          @size-change="onSizeChange"
          @current-change="onCurrentChange"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { oftenuse } from '@/libs/mixins'
import { getHasStockInBillList, cancleBill } from '@/api/bill/bill-stock-in.js'
import SearchFilter from '@/views/components/SearchFilter'
import Conditions from './conditions'
import { getMoreButton, getDelButton } from '@/components/Table/oper-btns.js'
import { getBusinessTime } from '@/libs/util.js'

export default {
  name: 'StockHasInList',
  filters: {
  },
  components: { SearchFilter, Conditions },
  mixins: [oftenuse],
  props: {
  },
  data() {
    return {
      searchData: { isCancle: '0', dateTab: 3 },
      optionsData: {},
      currentIndex: 0,
      isFilterOpen: false,
      columnsHasInData: [
        { title: '操作', width: 80,
          slots: {
            default: (params) => {
              const { row } = params
              const h = this.$createElement
              const { isCancle } = row
              const viewMoreNode = getMoreButton(h, { on: { click: () => { this.btnViewData(row) } }})
              const delBtnNode = getDelButton(h, {
                attrs: { title: '作废' },
                on: { click: () => { this.btnCancleData(row) } }
              })
              const divNode = h('div', {
                attrs: {
                  class: 'table-oper-btns'
                }
              }, [viewMoreNode, isCancle === '0' ? delBtnNode : null])
              return [divNode]
            }
          }
        },
        { field: 'billNo', title: '单据编号', width: 140 },
        { field: 'billRelationNo', title: '关联业务编号', width: 150 },
        { field: 'businessTime', title: '业务日期', width: 100,
          slots: {
            default: (params) => {
              const { row, column } = params
              return [getBusinessTime(row[column.property])]
            }
          }
        },
        { field: 'comegoName', title: '往来单位', width: 200, showOverflow: true },
        { field: 'goodsNamestr', title: '入库商品', width: 200, showOverflow: true },
        { field: 'goodsQuantity', title: '入库量', width: 150 }
      ]
    }
  },
  computed: {
    'btnFilterOpenText': function() {
      return this.isFilterOpen ? '收起筛选' : '展开筛选'
    }
  },
  watch: {
  },
  created() {
    this.listData()
  },
  methods: {
    btnCancleData(row) {
      const billId = row.id
      this.isAllowDel('确定要作废该单据吗？').then(_ => {
        this.cancleBill(billId)
      })
    },
    btnTransData(row) {
      const billId = row.id
      this.$router.push({
        name: 'StockinAdd',
        query: { billId }
      })
    },
    btnViewData(row) {
      const { id } = row
      const params = { billId: id }
      this.$router.push({
        name: 'StockInHasDetailView',
        query: params
      })
    },
    btnSearch() {
      this.setBtnLoading('search', true)
      this.pageInfo.current = 1
      this.listData().then(res => {
        this.setBtnLoading('search', false)
      }).catch(err => {
        console.log(err)
        this.setBtnLoading('search', false)
      })
    },
    btnClearConditions() {
      this.$refs.conditions.clearData()
    },
    // api
    listData(params = { data: this.searchData, page: this.pageInfo }) {
      if (params.data.date) {
        const data = params.data
        data.beginDate = data.date[0]
        data.endDate = data.date[1]
      }
      this.setTableLoading('table', true)
      return new Promise((resolve, reject) => {
        getHasStockInBillList(params).then(res => {
          this.setTableLoading('table', false)
          if (!res.data.flag) return
          const { records, total } = res.data.data
          this.setTableData('table', records)
          this.pageInfo.total = total
          resolve(res)
        }).catch(err => {
          reject(err)
          this.setTableLoading('table', false)
        })
      })
    },
    cancleBill(billId) {
      return cancleBill(billId).then(res => {
        if (!res.data.flag) return this.$notify.error({ title: '作废失败！', message: res.data.message })
        this.$notify.success(res.data.message || '作废成功！')
        this.listData()
      })
    },
    // on
    onSizeChange(val) {
      this.pageInfo.pagx1e = 1
      this.pageInfo.size = val
      this.listData()
    },
    onCurrentChange(val) {
      this.pageInfo.current = val
      this.listData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">

</style>
